<template>
  <div class="Role">
    <el-dialog title="角色管理" :visible.sync="isShow" @close="closeEvent">
      <el-checkbox-group v-model="checkList">
        <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{
          item.name
        }}</el-checkbox>
      </el-checkbox-group>
      <template #footer>
        <div style="text-align: center">
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="closeEvent">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { sysRole } from '@/api/setting'
import { sysUser } from '@/api/user'
import { sysUserAssignRoles } from '@/api/employees'
export default {
  data() {
    return {
      isShow: false,
      checkList: [],
      roleList: [],
      id: ''
    }
  },
  async created() {
    const res = await sysRole({ page: 1, pagesize: 9999 })
    this.roleList = res.data.rows
  },
  methods: {
    async getRoleIds(id) {
      this.id = id
      const res = await sysUser(id)
      this.checkList = res.data.roleIds
    },
    closeEvent() {
      this.isShow = false
      // 关闭弹框时清除勾选项
      this.checkList = []
    },
    async submit() {
      await sysUserAssignRoles(this.id, this.checkList)
      this.$message.success('分配角色成功')
      this.closeEvent()
    }
  }
}
</script>

<style></style>
